home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / src / timer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-24  |  3.0 KB  |  156 lines

  1. /* @(#) $Header: timer.c,v 1.7 91/05/24 12:10:25 deyke Exp $ */
  2.  
  3. /* General purpose software timer facilities
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include "global.h"
  9. #include "timer.h"
  10.  
  11. extern int gettimeofday __ARGS((struct timeval *tp, struct timezone *tzp));
  12.  
  13. int32 Msclock;
  14. int32 Secclock;
  15.  
  16. /* Head of running timer chain.
  17.  * The list of running timers is sorted in increasing order of expiration;
  18.  * i.e., the first timer to expire is always at the head of the list.
  19.  */
  20. static struct timer *Timers;
  21.  
  22. /* Process that handles clock ticks */
  23. void
  24. timerproc()
  25. {
  26.     register struct timer *t;
  27.     int32 bugfix;
  28.     struct timeval tv;
  29.     struct timezone tz;
  30.  
  31.     gettimeofday(&tv, &tz);
  32.     Secclock = tv.tv_sec;
  33.     Msclock = 1000 * Secclock + tv.tv_usec / 1000;
  34.  
  35.     while((t = Timers) && (bugfix = t->expiration - Msclock) <= 0) {
  36.         if (Timers = t->next)
  37.             Timers->prev = NULLTIMER;
  38.         t->state = TIMER_EXPIRE;
  39.         if(t->func)
  40.             (*t->func)(t->arg);
  41.     }
  42. }
  43. /* Start a timer */
  44. void
  45. start_timer(t)
  46. struct timer *t;
  47. {
  48.     register struct timer *tnext;
  49.     struct timer *tprev = NULLTIMER;
  50.     int32 bugfix;
  51.  
  52.     if(t == NULLTIMER)
  53.         return;
  54.     if(t->state == TIMER_RUN)
  55.         stop_timer(t);
  56.     if(t->duration <= 0)
  57.         return;         /* A duration value of 0 disables the timer */
  58.  
  59.     t->expiration = Msclock + t->duration;
  60.     t->state = TIMER_RUN;
  61.  
  62.     /* Find right place on list for this guy. Once again, note use
  63.      * of subtraction and comparison with zero rather than direct
  64.      * comparison of expiration times.
  65.      */
  66.     for(tnext = Timers;tnext != NULLTIMER;tprev=tnext,tnext = tnext->next){
  67.         if((bugfix = tnext->expiration - t->expiration) >= 0)
  68.             break;
  69.     }
  70.     /* At this point, tprev points to the entry that should go right
  71.      * before us, and tnext points to the entry just after us. Either or
  72.      * both may be null.
  73.      */
  74.     if((t->prev = tprev) == NULLTIMER)
  75.         Timers = t;             /* Put at beginning */
  76.     else
  77.         tprev->next = t;
  78.  
  79.     if (t->next = tnext)
  80.         tnext->prev = t;
  81. }
  82. /* Stop a timer */
  83. void
  84. stop_timer(timer)
  85. struct timer *timer;
  86. {
  87.  
  88.     if(timer == NULLTIMER || timer->state != TIMER_RUN)
  89.         return;
  90.  
  91.     if(timer->prev)
  92.         timer->prev->next = timer->next;
  93.     else
  94.         Timers = timer->next;   /* Was first on list */
  95.  
  96.     if(timer->next)
  97.         timer->next->prev = timer->prev;
  98.  
  99.     timer->state = TIMER_STOP;
  100. }
  101. /* Return milliseconds remaining on this timer */
  102. int32
  103. read_timer(t)
  104. struct timer *t;
  105. {
  106.     int32 remaining;
  107.  
  108.     if(t == NULLTIMER || t->state != TIMER_RUN)
  109.         return 0;
  110.  
  111.     if((remaining = t->expiration - Msclock) <= 0)
  112.         return 0;       /* Already expired */
  113.     else
  114.         return remaining;
  115. }
  116. int32
  117. next_timer_event()
  118. {
  119.     if (Timers)
  120.         return read_timer(Timers);
  121.     else
  122.         return 0x7fffffff;
  123. }
  124. /* Convert time count in seconds to printable days:hr:min:sec format */
  125. char *
  126. tformat(t)
  127. int32 t;
  128. {
  129.     static char buf[17],*cp;
  130.     unsigned int days,hrs,mins,secs;
  131.     int minus;
  132.  
  133.     if(t < 0){
  134.         t = -t;
  135.         minus = 1;
  136.     } else
  137.         minus = 0;
  138.  
  139.     secs = t % 60;
  140.     t /= 60;
  141.     mins = t % 60;
  142.     t /= 60;
  143.     hrs = t % 24;
  144.     t /= 24;
  145.     days = t;
  146.     if(minus){
  147.         cp = buf+1;
  148.         buf[0] = '-';
  149.     } else
  150.         cp = buf;
  151.     sprintf(cp,"%u:%02u:%02u:%02u",days,hrs,mins,secs);
  152.  
  153.     return buf;
  154. }
  155.  
  156.